package com.cskaoyan.service.wx.hx;

import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.bo.VXCommentListBO;
import com.cskaoyan.module.pojo.MarketComment;
import com.cskaoyan.module.pojo.MarketCommentExample;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.vo.wx.hx.CommentCountVO;
import com.cskaoyan.module.vo.wx.hx.ListEntity;
import com.cskaoyan.module.vo.wx.hx.UserInfoEntity;
import com.cskaoyan.module.vo.wx.hx.VXCommentListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/04/11 17:29
 *
 * @author Administrator
 */
@Service
public class VXCommentServiceImpl implements VXCommentService {


    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public BaseRespVo commentList(VXCommentListBO vxCommentListBO) {
        // 获取userid
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();

        Integer valueId = vxCommentListBO.getValueId();
        Integer limit = vxCommentListBO.getLimit();
        Integer page = vxCommentListBO.getPage();
        Integer showType = vxCommentListBO.getShowType();
        Integer type = vxCommentListBO.getType();

        // 分页插件 PageHelper,辅助我们作分页和分页信息的获得
        //执行查询过程中拼接分页信息
        PageHelper.startPage(page, limit);
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);

        // 设置返回的userInfo
        MarketUser marketUser1 = marketUserMapper.selectByPrimaryKey(id);
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setAvatarUrl(marketUser1.getAvatar());
        userInfoEntity.setNickName(marketUser1.getNickname());

        ArrayList<ListEntity> list = new ArrayList<>();
        ArrayList<ListEntity> list2 = new ArrayList<>();
        // showType == 0 表示显示全部的评论
        if (showType == 0) {
            for (MarketComment marketComment : marketComments) {
                ListEntity listEntity = new ListEntity();
                listEntity.setAddTime(marketComment.getAddTime());
                listEntity.setAdminContent(marketComment.getAdminContent());
                listEntity.setContent(marketComment.getContent());
                listEntity.setPicList(marketComment.getPicUrls());
                listEntity.setUserInfo(userInfoEntity);
                list.add(listEntity);
             }

            PageInfo<ListEntity> pageInfo = new PageInfo<>(list);
            int pages = pageInfo.getPages();
            long total = pageInfo.getTotal();
            VXCommentListVO vxCommentListVO = new VXCommentListVO(total, pages, limit, page, list);


            return BaseRespVo.ok(vxCommentListVO);
        }
        for (MarketComment marketComment : marketComments) {
            if (marketComment.getHasPicture()) {
                ListEntity listEntity = new ListEntity();
                listEntity.setAddTime(marketComment.getAddTime());
                listEntity.setAdminContent(marketComment.getAdminContent());
                listEntity.setContent(marketComment.getContent());
                listEntity.setPicList(marketComment.getPicUrls());
                listEntity.setUserInfo(userInfoEntity);
                list2.add(listEntity);
            }

        }

        PageInfo<ListEntity> pageInfo = new PageInfo<>(list2);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        VXCommentListVO vxCommentListVO = new VXCommentListVO(total, pages, limit, page, list2);


        return BaseRespVo.ok(vxCommentListVO);
    }

    @Override
    public BaseRespVo countComment(MarketComment marketComment) {
        Integer valueId = marketComment.getValueId();
        Byte type = marketComment.getType();
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
        CommentCountVO countVO = new CommentCountVO();

        int i = 0;
        int j = 0;
        for (MarketComment comment : marketComments) {
                j++;

            if (comment.getHasPicture()){
                i++;
            }

        }
        countVO.setAllCount(j);
        countVO.setHasPicCount(i);
        return BaseRespVo.ok(countVO);
    }

    @Override
    public BaseRespVo postComment(MarketComment marketComment) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();

        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        marketComment.setUserId(id);
        int insert = marketCommentMapper.insertSelective(marketComment);

        System.out.println(insert);
        int i = marketCommentMapper.selectMaxId();

        System.out.println(i);

        MarketComment marketComment1 = marketCommentMapper.selectByPrimaryKey(i);



        return BaseRespVo.ok(marketComment1);
    }
}



